package org.gradle;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

public class WebSocketDncoders implements Decoder.Text<Message>{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Message decode(String s) throws DecodeException {
        return JSON.parseObject(s, Message.class);
    }

    @Override
    public boolean willDecode(String s) {
        try {
            // Check if incoming message is valid JSON
            // 解码之前处理
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public void init(EndpointConfig config) {
        logger.info("MessageDecoder -init method called");
    }

    @Override
    public void destroy() {
        logger.info("MessageDecoder - destroy method called");
    }
}
